﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Kugar.Core.ExtMethod;
using Kugar.UI.Web.AdminLTE.Helpers;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageForms
{
    public class PageForm_SelectorField: PageForm_SingleInputFieldBase
    {
        //private string _name = "";
        private IEnumerable<SelectorItem> _options = null;
        private SelectorItem _firstItem = null;
        private string _selectedValue = "";
        private string _onChangedScript = "";
        private bool _disabled;
        private bool _autoSubmit = false;
        private bool _isMultiSelect = false;
         
        public PageForm_SelectorField(PageForm_Form form, ViewContext viewContext) : base(form, viewContext)
        {
            this.Css("webui-form_selectorfield");
        }

        public PageForm_SelectorField  Name(string name)
        {
            _name = name;

            return this;
        }

        /// <summary>
        /// 修改时,触发的事件处理语句
        /// </summary>
        /// <param name="changedScript"></param>
        /// <returns></returns>
        public PageForm_SelectorField OnChangedScript(string changedScript)
        {
            _onChangedScript = changedScript;

            return this;
        }

        public PageForm_SelectorField CurrentValue(string selectedValue)
        {
            _selectedValue = selectedValue;

            return this;
        }

        public PageForm_SelectorField CurrentValue(int selectedValue)
        {
            _selectedValue = selectedValue.ToStringEx();

            return this;
        }

        public PageForm_SelectorField Options(params SelectorItem[] options)
        {
            _options = options;

            return this;
        }

        public PageForm_SelectorField Options(params (string title, string value)[] options)
        {
            _options = options.Select(x=>new SelectorItem(x.title,x.value)).ToArrayEx();

            return this;
        }

        public PageForm_SelectorField Options(params (string title, int value)[] options)
        {
            _options = options.Select(x => new SelectorItem(x.title, x.value.ToStringEx())).ToArrayEx();

            return this;
        }

        /// <summary>
        /// 在末尾增加一个选项
        /// </summary>
        /// <param name="title"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public PageForm_SelectorField AppendOption(string title, string value)
        {
            _options = _options.Concat(new[] {new SelectorItem(title, value)}).ToArrayEx();

            return this;
        }

        /// <summary>
        /// 是否允许多选
        /// </summary>
        /// <param name="isMultiSelect"></param>
        /// <returns></returns>
        public PageForm_SelectorField MultiSelect(bool isMultiSelect = true)
        {
            _isMultiSelect = isMultiSelect;

            return this;
        }

        public PageForm_SelectorField Options<TEnumableData>(IEnumerable<TEnumableData> optionsGetter, Func<TEnumableData, string> titleGetter, Func<TEnumableData, string> valueGetter, Func<TEnumableData, bool> disabledGetter = null)
        {
            _options = optionsGetter.Select(x => new SelectorItem(titleGetter(x), valueGetter(x), disabledGetter?.Invoke(x) ?? false));

            return this;
        }

        /// <summary>
        /// 选项列表,根据枚举类型
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public PageForm_SelectorField Options(Type enumType, Func<SelectorItem, bool> disabledGetter = null)
        {
            if (!enumType.IsEnum)
            {
                throw new ArrayTypeMismatchException("enumType参数类型必须为枚举类型");
            }
            _options = Utility.GetEnumTypeToSelectorItems(enumType);

            if (disabledGetter!=null)
            {
                foreach (var opt in _options)
                {
                    opt.Disabled = disabledGetter(opt);
                }
            }
            

            return this;
        }

        /// <summary>
        /// 额外真在Options首个备选项前的选项
        /// </summary>
        /// <param name="firstItem"></param>
        /// <returns></returns>
        public PageForm_SelectorField FirstItem(SelectorItem firstItem)
        {
            _firstItem = firstItem;

            return this;
        }

        public PageForm_SelectorField FirstItem(string title, string value)
        {
            _firstItem = new SelectorItem(title, value);

            return this;
        }

        //public PageForm_SelectorField AutoSubmit(bool autoSubmit=true)
        //{
        //    _autoSubmit = autoSubmit;

        //    return this;
        //}

        public PageForm_SelectorField Disabled(bool disabled = true)
        {
            _disabled = disabled;

            return this;
        }

        protected override void BeforeRender(TextWriter writer)
        {
            if (_isMultiSelect)
            {
                this.Css("multipleselect");
            }
            

            base.BeforeRender(writer);
        }

        protected override void RenderContent(TextWriter writer)
        {
            writer.WriteLine($"<select lay-ignore class=\"form-control\" {_isMultiSelect.If("multiple")} data-autosubmit=\"{_autoSubmit}\" onchange='{_onChangedScript}' name=\"{_name}\" {_disabled.If("disabled")} >");

            if (_firstItem != null)
            {
                writer.WriteLine($"<option value=\"{_firstItem.Value}\" {_firstItem.Disabled.If("disabled")}>{_firstItem.Title}</option>");
            }

            foreach (var option in _options)
            {
                writer.WriteLine($"<option value=\"{option.Value}\" {(_selectedValue == option.Value ? "selected" : "")} {option.Disabled.If("disabled")}>{option.Title}</option>");
            }

            writer.WriteLine("</select>");
            
        }
    }
}
